package com.goldze.mvvmhabit.ui.food;


import android.arch.lifecycle.ViewModelProviders;
import android.os.Bundle;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.text.TextUtils;
import android.view.View;

import com.goldze.mvvmhabit.R;
import com.goldze.mvvmhabit.adapter.FoodAdapter;
import com.goldze.mvvmhabit.app.AppViewModelFactory;
import com.goldze.mvvmhabit.data.FoodBean;
import com.goldze.mvvmhabit.databinding.ActivityFoodBinding;
import com.goldze.mvvmhabit.listener.OnItemClickListener;
import com.goldze.mvvmhabit.okgo.HTTPAPI;
import com.goldze.mvvmhabit.utils.GsonUtil;
import com.goldze.mvvmhabit.utils.SpUtils;
import com.lzy.okgo.callback.StringCallback;

import java.util.ArrayList;
import java.util.List;

import me.goldze.mvvmhabit.base.BaseActivity;
import me.goldze.mvvmhabit.utils.LogUtil;
import me.goldze.mvvmhabit.utils.ToastUtils;
import okhttp3.Call;
import okhttp3.Response;

/**
 * 一日三餐
 */
public class FoodActivity extends BaseActivity<ActivityFoodBinding, FoodViewModel> {

    public FoodAdapter adapter;
    public List<FoodBean.ResultBean> listFoodBeans = new ArrayList<>();
    public String type = "first";
    public int number = 0;
    public int allHot = 0;
    public String detailStr = "",updateFoodStr = "";

    //ActivityLoginBinding类是databinding框架自定生成的,对应activity_login.xml
    @Override
    public int initContentView(Bundle savedInstanceState) {
        return R.layout.activity_food;
    }

    @Override
    public int initVariableId() {
        return com.goldze.mvvmhabit.BR.viewModel;
    }

    @Override
    public FoodViewModel initViewModel() {
        //使用自定义的ViewModelFactory来创建ViewModel，如果不重写该方法，则默认会调用LoginViewModel(@NonNull Application application)构造方法
        AppViewModelFactory factory = AppViewModelFactory.getInstance(getApplication());
        return ViewModelProviders.of(this, factory).get(FoodViewModel.class);
    }

    @Override
    public void initViewObservable() {
        binding.rlModule.setLayoutManager(new LinearLayoutManager(FoodActivity.this));
        binding.rlModule.setItemAnimator(new DefaultItemAnimator());
        adapter = new FoodAdapter(this,listFoodBeans);
        binding.rlModule.setAdapter(adapter);
        adapter.setOnItemClickLitener(new OnItemClickListener() {
            @Override
            public void onItemClick(View view, int position) {
                if (!TextUtils.isEmpty(listFoodBeans.get(position).getNumber())) {
                    number = Integer.valueOf(listFoodBeans.get(position).getNumber());
                    LogUtil.e("食物列表数量-->"+ number);
                    listFoodBeans.get(position).setNumber(String.valueOf(++number));
                }else{
                    listFoodBeans.get(position).setNumber("1");
                }
                adapter.notifyItemChanged(position);

                allHot = 0;
                detailStr = "";
                updateFoodStr = "";
                for(FoodBean.ResultBean resultBean : listFoodBeans){
                    if (!TextUtils.isEmpty(resultBean.getNumber())) {
                        allHot += Integer.valueOf(resultBean.getHeat()) * Integer.valueOf(resultBean.getNumber());
                        LogUtil.e(resultBean.getName() + "数量"+ resultBean.getNumber());
                        detailStr += resultBean.getName() + resultBean.getNumber() + resultBean.getSpec() + ";";
                        updateFoodStr += resultBean.getId() + "-" + resultBean.getNumber() + ",";
                    }
                }

                LogUtil.e("总热量-->"+ allHot + "卡路里");

                switch (type){
                    case "first":
                        binding.tvModuleFirst.setText("总热量"+ allHot + "其中" + detailStr);
                        break;
                    case "second":
                        binding.tvModuleSecond.setText("总热量"+ allHot + "其中" + detailStr);
                        break;
                    case "third":
                        binding.tvModuleThird.setText("总热量"+ allHot + "其中" + detailStr);
                        break;
                    default:
                }


            }

            @Override
            public void onItemLongClick(View view, int position) {

            }
        });

        type = getIntent().getStringExtra("module_type");
        switch (type){
            case "first":
                binding.llModuleFirst.setVisibility(View.VISIBLE);
                binding.llModuleSecond.setVisibility(View.GONE);
                binding.llModuleThird.setVisibility(View.GONE);
                break;
            case "second":
                binding.llModuleFirst.setVisibility(View.GONE);
                binding.llModuleSecond.setVisibility(View.VISIBLE);
                binding.llModuleThird.setVisibility(View.GONE);
                break;
            case "third":
                binding.llModuleFirst.setVisibility(View.GONE);
                binding.llModuleSecond.setVisibility(View.GONE);
                binding.llModuleThird.setVisibility(View.VISIBLE);
                break;
                default:
        }

        //获取食物列表
        getFoodList();

        //提交
        binding.tvModuleUp.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                payHot();
            }
        });

    }

    //获取食物列表
    private void getFoodList() {
        String token = (String) SpUtils.get("module_token","");

        HTTPAPI.getInstance().getFoodList(token,new StringCallback() {
            @Override
            public void onSuccess(String s, Call call, Response response) {
                LogUtil.e("食物-->" + s);
                FoodBean foodBean = GsonUtil.INSTANCE.parse(s,FoodBean.class);
                if (foodBean.getCode() == 200) {
                    listFoodBeans.clear();
                    listFoodBeans.addAll(foodBean.getResult());
                    adapter.notifyDataSetChanged();
                }
            }
        });

    }


    //提交食物
    public void payHot(){

        if (TextUtils.isEmpty(updateFoodStr)) {
            ToastUtils.showShort("请选择食物");
            return;
        }

        String token = (String) SpUtils.get("module_token","");
        String personId = (String) SpUtils.get("module_person_id", "");

        LogUtil.e("提交数据-->"+ updateFoodStr);

        HTTPAPI.getInstance().postFood(token,updateFoodStr.substring(0,updateFoodStr.length() - 1),personId,new StringCallback() {
            @Override
            public void onSuccess(String s, Call call, Response response) {
                LogUtil.e("提交食物-->" + s);
                ToastUtils.showShort("提交成功");
                finish();
            }
        });
    }


}

